package com.craftworks.tiles.impl;

import cn.hutool.core.date.ChineseDate;
import com.craftworks.tiles.AbstractTile;

import java.awt.*;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.util.List;
import java.util.Locale;

public class DateTile extends AbstractTile
{
    private LocalDate date;
    private FontMetrics fmDate;
    private FontMetrics fmWeekday;
    private FontMetrics fmLunarDate;

    private static final Font FONT_DATE = new Font("Source Han Sans CN", Font.PLAIN, 16);
    private static final Font FONT_WEEKDAY = new Font("Source Han Sans CN", Font.PLAIN, 30);
    private static final Font FONT_LUNAR_DATE = new Font("Source Han Sans CN", Font.PLAIN, 14);
    private static final Color WEEKEND = Color.decode("#ff001e");
    private static final Color BACKGROUND = Color.decode("#18181A");
    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy/MM/dd");

    public DateTile()
    {
        super(Size.LANDSCAPE);
    }

    @Override
    public List<String> propertyKeys()
    {
        return List.of("date");
    }

    @Override
    public void setProperty(String key, Object value)
    {
        if ("date".equals(key))
        {
            switch (value)
            {
                case null -> date = LocalDate.now();
                case LocalDate d -> date = d;
                case String s -> date = LocalDate.parse(s);
                default -> throw new IllegalStateException("Unexpected value: " + value);
            }
            repaint();
        }
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        g2d.setPaint(BACKGROUND);
        g2d.fillRect(0, 28, size.width, 46);

        if (date == null)
            date = LocalDate.now();

        if (fmDate == null)
            fmDate = g2d.getFontMetrics(FONT_DATE);
        String dateStr = date.format(FORMATTER);
        int width = fmDate.stringWidth(dateStr);
        g2d.setFont(FONT_DATE);
        g2d.setPaint(FOREGROUND);
        g2d.drawString(dateStr, (size.width - width) / 2, 20);

        boolean isWeekend = (date.getDayOfWeek() == DayOfWeek.SATURDAY || date.getDayOfWeek() == DayOfWeek.SUNDAY);

        if (fmWeekday == null)
            fmWeekday = g2d.getFontMetrics(FONT_WEEKDAY);
        String weekday = date.getDayOfWeek().getDisplayName(TextStyle.FULL_STANDALONE, Locale.CHINA);
        width = fmWeekday.stringWidth(weekday);
        g2d.setFont(FONT_WEEKDAY);
        g2d.setPaint(isWeekend ? WEEKEND : FOREGROUND);
        g2d.drawString(weekday, (size.width - width) / 2, 62);

        ChineseDate chineseDate = new ChineseDate(date);

        if (fmLunarDate == null)
            fmLunarDate = g2d.getFontMetrics(FONT_LUNAR_DATE);
        String lunar = chineseDate.getChineseMonth() + chineseDate.getChineseDay();
        width = fmLunarDate.stringWidth(lunar);
        g2d.setFont(FONT_LUNAR_DATE);
        g2d.setPaint(FOREGROUND);
        g2d.drawString(lunar, (size.width - width) / 2, 92);
    }
}
